package com.aasa.siccosap.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.apache.log4j.Logger;

import com.aasa.siccosap.bean.ObraNotaCredito;
import com.aasa.siccosap.bean.ParametrosConsultaObra;
import com.aasa.siccosap.util.Constantes;

public class ObraNotaCreditoDAO extends ConsultaObraBaseDAO{
	
	static Logger logger = Logger.getLogger("ObraNotaCreditoDAO.class");
	public ObraNotaCreditoDAO()	throws accesoDaoException {
		super();
	}
	
	public ObraNotaCreditoDAO(DataSource dataSource) {
		super(dataSource);
	}
	
	public List<ObraNotaCredito> consultarNotasCreditoObraSeleccionada(ParametrosConsultaObra parametros) throws Exception{
        ResultSet rs = null;
        this.av_message = null;
        this.storedProcedure = "begin CONSULTA_ACEDIM_PKG.P_CTACTE_OBRA_NCREDITO(?,?,?,?,?,?,?); end;";
        
        List<ObraNotaCredito> consultaNotasCreditoObra=new ArrayList<ObraNotaCredito>();
        
        try{
                this.conn = dataSource.getConnection();
                this.stmt = this.conn.prepareCall(storedProcedure);
                              
                configuraParametrosConsultaObra(parametros);
                
                this.stmt.execute();

                //Se obtiene la respuesta error/exito del stored
                this.av_message = (String)stmt.getObject(1);                	
                    if(this.av_message.trim().toUpperCase().endsWith(Constantes.NO_ERRORS)){
                        //Se obtiene el objeto del cursor
                        rs = (ResultSet)stmt.getObject(2);
                        while(rs.next()){
                        	ObraNotaCredito registroCtaCte = creaRegistroNotasCreditoObraSeleccionada(rs);
                        	consultaNotasCreditoObra.add(registroCtaCte);
                        }
                }else{
                    return null;
                }

        }catch (Exception e){
                e.printStackTrace();
                return null;
        }finally{
        		close(this.conn, rs, stmt);
        }
        return(consultaNotasCreditoObra);
        
    }
	
	public List<ObraNotaCredito> consultarNotasCreditoObraTodas(ParametrosConsultaObra parametros) throws Exception{
        ResultSet rs = null;
        this.av_message = null;
        this.storedProcedure = "begin CONSULTA_ACEDIM_PKG.P_CTACTE_OBRA_NCREDITO_GRAL(?,?,?,?,?,?,?); end;";
        
        List<ObraNotaCredito> consultaNotasCreditoObra=new ArrayList<ObraNotaCredito>();
        
        try{
                this.conn = dataSource.getConnection();
                this.stmt = this.conn.prepareCall(storedProcedure);
                              
                configuraParametrosConsultaObra(parametros);
                
                this.stmt.execute();

                //Se obtiene la respuesta error/exito del stored
                this.av_message = (String)stmt.getObject(1);                	
                    if(this.av_message.trim().toUpperCase().endsWith(Constantes.NO_ERRORS)){
                        //Se obtiene el objeto del cursor
                        rs = (ResultSet)stmt.getObject(2);
                        while(rs.next()){
                        	ObraNotaCredito registroCtaCte = creaRegistroNotasCreditoObraTodas(rs);
                        	consultaNotasCreditoObra.add(registroCtaCte);
                        }
                }else{
                    return null;
                }

               
        }catch (Exception e){
                e.printStackTrace();
                return null;
        }finally{
        		close(this.conn, rs, stmt);
        }
        return(consultaNotasCreditoObra);
        
    }

	
	private ObraNotaCredito creaRegistroNotasCreditoObraSeleccionada(ResultSet rs) throws SQLException {
		ObraNotaCredito bean = new ObraNotaCredito();
		
		bean.setStrPb_tipoDoc(rs.getString("DESC_TIP_DOC"));
		bean.setStrPb_serieManual(rs.getString("SERIE_MANUAL"));
		bean.setStrPb_numeroManual(rs.getString("NUMERO_MANUAL"));
		bean.setObjPb_fechaEmision(rs.getDate("FEC_EMI"));
		bean.setDblPb_toneladas(rs.getDouble("TONELADAS"));
		bean.setStrPb_moneda(rs.getString("MONEDA"));
		bean.setDblPb_importeDolares(rs.getDouble("MONTO_DOLARES"));
		bean.setDblPb_saldoDolares(rs.getDouble("SALDO_DOLARES"));
				
		return bean;
	}
	
	private ObraNotaCredito creaRegistroNotasCreditoObraTodas(ResultSet rs) throws SQLException {
		ObraNotaCredito bean = new ObraNotaCredito();
		
		bean.setStrPb_despacho(rs.getString("NUM_DESPACHO"));
		bean.setStrPb_serieManual(rs.getString("SERIE_MANUAL"));
		bean.setStrPb_numeroManual(rs.getString("NUMERO_MANUAL"));
		bean.setObjPb_fechaEmision(rs.getDate("FEC_EMI"));
		bean.setDblPb_toneladas(rs.getDouble("TONELADAS"));
		bean.setStrPb_moneda(rs.getString("MONEDA"));
		bean.setDblPb_importeDolares(rs.getDouble("MONTO_DOLARES"));
		bean.setDblPb_saldoDolares(rs.getDouble("SALDO_DOLARES"));
				
		return bean;
	}
}
